# View Lyrics: a Quod Libet plugin for viewing lyrics.
# Copyright (C) 2008, 2011 Vasiliy Faronov <vfaronov@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You can get a copy of the GNU General Public License at:
# http://www.gnu.org/licenses/gpl-2.0.html

"""Provides the `ViewLyrics` plugin for viewing lyrics in the main window."""

import os

import gtk

from quodlibet import widgets
from quodlibet.plugins.events import EventPlugin

class ViewLyrics(EventPlugin):

    """The plugin for viewing lyrics in the main window."""
    
    PLUGIN_ID = "View Lyrics"
    PLUGIN_NAME = _("View Lyrics")
    PLUGIN_DESC = _("View lyrics beneath the song list.")
    PLUGIN_VERSION = "0.2"
    
    def enabled(self):
        self.expander = gtk.expander_new_with_mnemonic(_("_Lyrics"))
        self.expander.set_expanded(True)
        
        self.scrolled_window = gtk.ScrolledWindow()
        self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC,
            gtk.POLICY_AUTOMATIC)
        self.scrolled_window.set_size_request(-1, 200)
        self.adjustment = self.scrolled_window.get_vadjustment()
        
        self.textview = gtk.TextView()
        self.textbuffer = self.textview.get_buffer()
        self.textview.set_editable(False)
        self.textview.set_cursor_visible(False)
        self.textview.set_wrap_mode(gtk.WRAP_WORD)
        self.textview.set_justification(gtk.JUSTIFY_CENTER)
        self.scrolled_window.add_with_viewport(self.textview)
        self.textview.show()
        
        self.expander.add(self.scrolled_window)
        self.scrolled_window.show()
        widgets.main.songpane.pack_start(self.expander, expand=False,
            fill=True)
        # We don't show the expander here because it will be shown when a song
        # starts playing (see plugin_on_song_started).
    
    def disabled(self):
        self.textview.destroy()
        self.scrolled_window.destroy()
        self.expander.destroy()
    
    def plugin_on_song_started(self, song):
        """Called when a song is started. Loads the lyrics.
        
        If there are lyrics associated with the song, loads them into the
        lyrics viewer. Otherwise, hides the lyrics viewer.
        
        """
        if (song is not None) and os.path.exists(song.lyric_filename):
            with file(song.lyric_filename) as lyric_file:
                self.textbuffer.set_text(lyric_file.read())
            self.adjustment.set_value(0)    # Scroll to the top.
            self.expander.show()
        else:
            self.expander.hide()
